package core.reports;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * This implementation of IReportVisitor saves the table in HTML format.
 * @author Royce
 *
 */
public class HTMLBuilder implements IReportBuilder {

	private int numCols;
	private int curCol;
	private int curRow;
	private int tablesPrinted;
	
	private boolean fileOpen;
	private boolean tableOpen;
	
	private PrintWriter out;
	
	private String _fileName;
	
	private String endl = "\r\n";
	private String HTMLHeader = "<!DOCTYPE HTML>" + endl + "<html><head><meta charset=\"UTF-8\">" 
			+ "<title>Report</title><style>" +
			"h2{text-align:center;}" +
			"table#table1{width:100%;}" +
			"td.headerrow{font-weight:bold;}" +
			"</style></head><body>";
	private String HTMLFooter = "</body></html>";
	private String tableHeader = "<table id=\"table1\" border=\"1\">";
	private String tableFooter = "</table>";
	
	/**
	 * Default no-arg constructor
	 */
	private HTMLBuilder() {
		fileOpen = false;
		tableOpen = false;
		numCols = -1;
		curCol = 0;
		curRow = 0;
		tablesPrinted = 0;
		out = null;
		_fileName = "";
	}
	
	public HTMLBuilder(String fileName, String title) throws IOException {
		this();
		openFile(fileName, title);
	}
	
	@Override
	public void openFile(String fileName, String title) throws IOException {
		out = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
		fileOpen = true;
		out.println(HTMLHeader);
		out.println("<h2>" + title + "</h2>");
		_fileName = fileName;
	}

	@Override
	public void startTable(String title, int cols) throws IOException {
		if(!fileOpen) throw new IOException("HTMLBuilder: No file open.");
		assert(cols > 0);
		
		tableOpen = true;
		if (tablesPrinted > 0) {
			completeRow();
			out.println(tableFooter);
		}
		numCols = cols;
		if(!title.isEmpty()) {
			out.println("<h3>" + title + "</h3>");
		}
		out.println(tableHeader);
		tablesPrinted++;
		curCol = 0;
		curRow = 0;
	}

	@Override
	public void addCell(Object o) throws IOException {
		if(!fileOpen || !tableOpen)
			throw new IOException("HTMLBuilder: No table started.");
		
		if(curCol == 0) {
			out.println("<tr>");
		}
		
		String s = escapeHTML(o.toString());
		s = s.replaceAll("\r\n", "<br>");
		s = s.replaceAll("\n", "<br>");
		
		//out.print("<td style=\"width:" + tdWidth + "%\"");
		out.print("<td");
		if (curRow == 0) {
			out.println(" class=\"headerrow\">" + s + "</td>");
		}
		else {
			out.println(">" + s + "</td>");
		}
		curCol++;
		if (curCol == numCols) {
			out.println("</tr>");
			curCol = 0;
			curRow++;
		}

	}

	@Override
	public void completeRow() throws IOException {
		if(!fileOpen || !tableOpen) {
			return;
		}
		
		if(curCol == 0) return;
		
		while(curCol < numCols) {
			out.println("<td></td>");
			curCol++;
		}
		out.println("</tr>");
		curCol = 0;

	}

	@Override
	public void print() throws IOException {
		if(tableOpen)
			completeRow();
		out.println(tableFooter);
		out.println(HTMLFooter);
		out.flush();
		out.close();
		out = null;
		fileOpen = false;
		tableOpen = false;
		java.awt.Desktop.getDesktop().open(new File(_fileName));
		_fileName = "";
	}

	@Override
	public void addParagraph(String s, boolean isSubHeader) throws IOException {
		if (!fileOpen) throw new IOException("No file open!");
		
		s = s.replaceAll("\r\n", "<br>");
		s = s.replaceAll("\n", "<br>");
		
		if(isSubHeader) 
			out.println("<p><h3>" + s + "</h3></p>");
		else 
			out.println("<p>" + s + "</p>");
		
	}
	
	private String escapeHTML(String s) {
		String escaped = s;
		
		escaped = escaped.replace("&", "&amp;");
		escaped = escaped.replace(">", "&gt;");
		escaped = escaped.replace("<", "&lt;");
		escaped = escaped.replace("'", "&#039;");
		escaped = escaped.replace("\"", "&#34;");
		
		return escaped;
	}

}
